/********************************************************************
 
 Copyright (C), 2012年 dhc. All rights reserved.
 
 Filename: 	    NavigationViewController.m
 
 Author:        muzongcun
 
 Version:       1.0
 
 Description:   NavigationViewController
 
 History:
 
 muzongcun    2012/3/10     1.0     Create
 
 *********************************************************************/

#import "NavigationViewController.h"

@implementation NavigationViewController
@synthesize motionDelegate = _motionDelegate;

//******************************************************
// Method:     initWithNibName
// Access:     public 
// Returns:    void
// Parameter:  nibNameOrNil
// Parameter:  nibBundleOrNil
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
        
    }
    return self;
}

//******************************************************
// Method:     didReceiveMemoryWarning
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

//******************************************************
// Method:     viewDidLoad
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blueColor];
    UIPanGestureRecognizer *pinGesture = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(userPinned:)];
    pinGesture.delegate = self;
    [self.view addGestureRecognizer: pinGesture];
    [pinGesture release];
}

//******************************************************
// Method:     userPinned
// Access:     public 
// Returns:    void
// Parameter:  gesture
// Note:	   用户手势
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void) userPinned: (UIGestureRecognizer *) gesture 
{
    UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *) gesture;
    if (gesture.state == UIGestureRecognizerStateEnded) 
    {
        CGPoint velocityPoint = [panGesture velocityInView: self.view];
        if (fabsf(velocityPoint.x) > 85) 
        {
            //NSLog(@"UIGestureRecognizerStateEnded=%f", self.view.frame.origin.x);
            if (menuShown) 
            {
                if (velocityPoint.x>0) 
                {
                    return;
                }
            } 
            else if (!menuShown) 
            {
                if(velocityPoint.x<0) 
                {
                    return;
                }
            }
            if ([_motionDelegate respondsToSelector: @selector(switchMotion)]) 
            {
                [_motionDelegate switchMotion];
            }
        }
        else 
        {
            if ([_motionDelegate respondsToSelector: @selector(endTouch)]) 
            {
                [_motionDelegate endTouch];
            }
        }
        
        return;
    }

    CGPoint translatePoint = [panGesture translationInView: self.view];
    NSLog(@"%f", translatePoint.x);
    if ([_motionDelegate respondsToSelector: @selector(fingerHonMoved:)]) 
    {
        if (menuShown == YES) 
        {
            [_motionDelegate fingerHonMoved: translatePoint.x+160];
        }
        else
        {
            [_motionDelegate fingerHonMoved: translatePoint.x];
        }
        
    }
}

//******************************************************
// Method:     viewDidUnload
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//******************************************************
// Method:     shouldAutorotateToInterfaceOrientation
// Access:     public 
// Returns:    void
// Parameter:  interfaceOrientation
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//******************************************************
// Method:     menuShown
// Access:     public 
// Returns:    void
// Parameter:  shown
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void) menuShown: (BOOL) shown 
{
    menuShown = shown;
    if (shown) 
    {
        if (tapGestrue == nil) 
        {
            tapGestrue = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(meTapped)];
        }
        [self.view addGestureRecognizer: tapGestrue];
    } 
    else 
    {
        [self.view removeGestureRecognizer: tapGestrue];
    }
}

//******************************************************
// Method:     meTapped
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void) meTapped 
{
    if ([_motionDelegate respondsToSelector: @selector(switchMotion)]) 
    {
        [_motionDelegate switchMotion];
    }
}
@end
